package demo

import com.glory.ycsyrpt.TranposUtils

import java.text.SimpleDateFormat

/**
 * @Program: ycsy
 * @Description: 每日合格率表
 * @Author: Ivan
 */
class EverydayqualifiedController {

    def dynamicQueryService
    def exportFileService

    def everydayqualified() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def todayStart = params.todayStart ? sdf.parse(params.selectTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def todayEnd = params.todayEnd ? sdf.parse(params.selectTime) : sdf.parse(sdf.format(new Date().clearTime()))
        SimpleDateFormat sdf2 = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def selectTime = params.selectTime ? sdf2.parse(params.selectTime) : sdf2.parse(sdf2.format(new Date().clearTime()))
        String strDate = sdf.format(new Date())
        params.selectTime = params.selectTime ?: sdf2.format(new Date());
        int strDateH = Integer.parseInt(strDate.substring(11, 13))
        int strDateM = Integer.parseInt(strDate.substring(14, 16))
        if (strDateH < 8) {
            // 昨天到今天
            Calendar ca1 = Calendar.getInstance()
            ca1.setTime(selectTime)
            ca1.add(Calendar.HOUR_OF_DAY, 8)
//            ca1.add(Calendar.MINUTE, 30)
            todayStart = ca1.getTime()
            Calendar ca2 = Calendar.getInstance()
            ca2.setTime(selectTime)
            ca2.add(Calendar.HOUR_OF_DAY, -16)
//            ca2.add(Calendar.MINUTE, 30)
            todayEnd = ca2.getTime()
        } else {
            // 今天到明天
            Calendar ca1 = Calendar.getInstance()
            ca1.setTime(selectTime)
            ca1.add(Calendar.HOUR_OF_DAY, 8)
//            ca1.add(Calendar.MINUTE, 30)
            todayStart = ca1.getTime()
            Calendar ca2 = Calendar.getInstance()
            ca2.setTime(selectTime)
            ca2.add(Calendar.HOUR_OF_DAY, 32)
//            ca2.add(Calendar.MINUTE, 30)
            todayEnd = ca2.getTime()
        }
        SimpleDateFormat sdf3 = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def startTime = params.startTime ? sdf3.parse(params.startTime) :sdf3.parse(sdf3.format(todayStart))
        def endTime = params.endTime ? sdf3.parse(params.endTime):sdf3.parse(sdf3.format(todayEnd))
        params.startTime = sdf3.format(startTime)
        params.endTime = sdf3.format(endTime)
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def tableHeadList = []
        def reportList = []
        def totalCount = 0
        def reportChart = []
        def dateList = []
        def sysQueryName
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            params.SYS_QUERY_NAME = 'everyDayQualifiedDateList'
            dateList = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'everyDayQualifiedList'
            reportList = dynamicQueryService.queryForList(params)
            reportList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', reportList)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            totalCount = reportList.size()

            params.SYS_QUERY_NAME = 'everyDayQualifiedChart'
            reportChart = dynamicQueryService.queryForList(params)
            reportChart = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', reportChart)
        }
        [
                pageSize      : pageSize,
                totalCount    : totalCount,
                pageSizeList  : pageSizeList,
                startTime     : params.startTime ?: '',
                endTime       : params.endTime ?: '',
                dateList      : dateList,
                tableHeadList : tableHeadList,
                reportList    : reportList,
                totalCount    : totalCount,
                reportChart   : reportChart,
                SYS_QUERY_NAME: sysQueryName
        ]
    }

    def expExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.startTime = URLDecoder.decode(params.startTime, 'UTF-8')
        params.endTime = URLDecoder.decode(params.endTime, 'UTF-8')
        params.SYS_QUERY_NAME = 'everyDayQualifiedDateList'
        def dateList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'everyDayQualifiedList'
        def reportList = dynamicQueryService.queryForList(params)
        reportList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', reportList)
        def tableHeadList = []
        if (reportList.size() > 0) {
            tableHeadList = reportList[0].keySet()
        }
        String filename = new String("everyDayQualifiedList.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8")
        def result = g.render(template: "exp${params.EXPORT_CONTENT_NAME}", model: [dateList: dateList, reportList: reportList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
        exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", result)
    }
}